﻿using HRMSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HRMSystem.DBServices
{
    class DepartmentService:GenericService<Department>
    {
        public static List<DepartmentDetails> findDeparmentDetails()
        {
            try
            {
                var query = (from d in db.Departments
                             join b in db.Branches on d.ID equals b.COMPANY_ID
                             join e in db.Departments on b.ID equals e.BRANCH_ID
                             
                             select new DepartmentDetails()
                             {
                                 DID=d.ID,
                                 NAME = d.NAME,
                                 BRANCH_ID=d.BRANCH_ID
                                 
                             }).ToList();
                return query;
            }


            catch (Exception)
            {
                return null;
            }
        }

        internal static void insertDepartment(string name, string comment)
        {
            try
            {
                var query = from dept in db.Departments
                            select new { dept };

                foreach (var add in query)
                {
                    add.dept.NAME = name;
                   // add.dept.BRANCH_ID = branch_id;
                    add.dept.COMMENT = comment;


                }
                db.SaveChanges();
            }
            catch (Exception e)
            {
                throw new NotImplementedException();
            }
        }
        internal static void  updateDepartmentDetails(DepartmentDetails editDetails)
        {
            try
            {
                var query = (from d in db.Departments
                             join b in db.Branches on d.ID equals b.COMPANY_ID
                             join e in db.Departments on b.ID equals e.BRANCH_ID
                             select new { d, b, e });
                foreach (var add in query)
                {
                    add.d.NAME = editDetails.NAME;

                }
                db.SaveChanges();
            }


            catch (Exception e)
            {
                throw new NotImplementedException();
            }
        }

        internal static List<Department> GetDepartmentsByBranchId(int branchId)
        {
            try
            {
                return db.Departments.Where(d => d.BRANCH_ID.Equals(branchId) && d.ISDELETE == false).ToList(); 
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static Department GetDepartmentByDepartmentId(int departmentId)
        {
            try
            {
                return db.Departments.SingleOrDefault(d => d.ID.Equals(departmentId) && d.ISDELETE == false);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
